<?php

namespace lxs\tools;

use Illuminate\Support\Facades\Redis;

class WechatAccessToken implements \EasyWeChat\Kernel\Contracts\AccessToken
{
    public function getToken(): string
    {
        $accessToken = Redis::get('accessToken');
        if ($accessToken == null) {
            $app = MiniApp::getInstance();
            $accessToken = $app->getAccessToken()->getToken();
        }
        Redis::setEx('accessToken', 7200, $accessToken);
        return $accessToken;
    }

    public function toQuery(): array
    {
        return ['access_token' => $this->getToken()];
    }
}
